package part17;

/**
 * @author wcj
 * @Date 2021/6/1 19:42
 * @Version 1.0
 */
public class Part1711<K, V> {
    private Object[][] pairs;
    private int index;

    public Part1711(int length) {
        pairs = new Object[length][2];
    }

    public void put(K key, V val) {
        if (index >= pairs.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        pairs[index++] = new Object[]{key, val};
    }

    public V get(K key) {
        for (int i = 0; i < index; i++) {
            if (key.equals(pairs[i][0])) {
                return (V) pairs[i][1];
            }
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < index; i++) {
            stringBuilder.append(pairs[i][0].toString());
            stringBuilder.append(":");
            stringBuilder.append(pairs[i][1].toString());
            if (i < index - 1) {
                stringBuilder.append("\n");
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 实现的一个简单的map,利用泛型的思想
     * HashMap：实现hashCode()
     * LinkedHashMap:实现hashCode()
     * TreeMap:实现Comparable接口
     * ConcurrentHashMap
     * WeakHashMap
     *
     * @param args
     */
    public static void main(String[] args) {
        Part1711<String, String> map = new Part1711<>(6);
        map.put("wcj", "bss");
        map.put("ysj", "ylb");
        map.put("ywj", "ylb");
        map.put("yej", "ylb");
        map.put("ytj", "ylb");
        map.put("ypj", "ylb");

        System.out.println(map);
    }
}
